package light.head.dao.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.head.cache.Cache;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.model.RoleHandle;
import light.head.model.RoleHandlePK;

import org.springframework.stereotype.Service;

@Service
public class RoleHandleDAO extends BaseDAO<RoleHandle, RoleHandlePK> {

	private final static String sql_loadHandleMapByRoleCode = "FROM RoleHandle rh WHERE rh.roleCode = ?";
	private final static String sql_deleteByRoleCode = "DELETE RoleHandle rh WHERE rh.roleCode = ?";
	
	/**
	 * 根据权限编号获取该权限可以做的操作
	 * @param	权限编号
	 */
	public Map<String, Boolean> getHandleMapByRoleCode(Integer roleCode) {
		if (null == roleCode)
			return null;
		Map<String, Boolean> map = Cache.roleMap.get(roleCode);
		if (null == map)
			map = loadHandleMapByRoleCode(roleCode);
		return map;
	}
	
	/**
	 * 加载指定角色的所有操作到缓存
	 * @param roleCode
	 * @return
	 */
	public Map<String, Boolean> loadHandleMapByRoleCode(Integer roleCode) {
		List<RoleHandle> list = find(sql_loadHandleMapByRoleCode, roleCode);
		if (null == list || list.size() == 0)
			return null;
		
		Map<String, Boolean> result = new HashMap<String, Boolean>();
		for (RoleHandle ph : list)
			result.put(ph.getHandleCode(), true);
		Cache.roleMap.put(roleCode, result);
		return result;
	}
	
	/**
	 * 删除与指定权限编号相关的所有权限操作对象
	 * @param	权限编号
	 */
	public RESULT deleteByRoleCode(Integer roleCode) {
		execute(sql_deleteByRoleCode, roleCode);
		return RESULT.SUCCESS;
	}
}
